// miniprogram/pages/admin/client/client.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serviceTypeList: ['共享版', '定制版'],
    skip: 0,
    latest:1,
    items:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.login(user => {
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }
      this.setData({
        user: user
      });
      let where = {
        promotion: user.phone
      }
      if (options.role == 0) {
        where = {}
      } else {
        wx.setNavigationBarTitle({
          title: '我推广的工作室',
        })
      }
      this.loadItems(where);
    });

  },

  loadItems(where) {
    const that = this;
    wx.showNavigationBarLoading();
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        op: 'get',
        table: 'studio',
        skip: that.data.skip,
        where: where
        // orderBy: [{'updatetime': desc}]
      },
      success: res => {
        console.log(res)
        let latest = res.result.data.length,
          items = that.data.items
          that.data.latest = latest
        items = items.concat(res.result.data)
        that.setData({
          items
        })
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      }
    })

  },

  showActions(e) {
    const that = this;
    const index = e.currentTarget.dataset.index; // TODO
    const actionindex = parseInt(e.currentTarget.dataset.actionindex);
    const item = this.data.items[index];
    switch (actionindex) {
      case 0:
        const phone = item.contact.phone;
        if (phone) {
          wx.makePhoneCall({
            phoneNumber: phone
          }).then(res => {
            console.log(res);
          }).catch(e => {})
        }
        break;
      case 1:
        // 跳转修改
        that.setData({
          modalname: 'edit',
          studio: item
        })
        break;
      case 2:
        let path = `pages/user/index/index?studioid=${item._id}`
        wx.navigateToMiniProgram({
          appId: item.appid,
          extraData: {
            studioid: item._id
          },
          envVersion: "trial",
          path: path,
        })
        break;
      case 3:
        wx.setClipboardData({
          data: item.wechat,
          success(res) {
            //  wx.hideToast(); //隐藏复制成功的弹窗提示,根据需求可选
          }
        })

        break;
      case 4:
        // 删除
        that.removeItem(item)
        break;
    }

  },


  search: function (e) {
    const form = e.detail.value;
    const that = this;
    if (!form.title) {
      this.loadItems();
      return;
    }
    wx.showNavigationBarLoading();
    // wx.cloud.callFunction({
    //   name: 'studio',
    //   data: {
    //     op: 'search',
    //     studioid: app.globalData.user.studioid,
    //     name: form.name
    //     // orderBy: [{'updatetime': desc}]
    //   },
    //   success: res => {
    //     console.log(res.result);
    //     that.setData({
    //       items: res.result.list
    //     })
    //     wx.hideNavigationBarLoading();
    //     //停止下拉刷新
    //     wx.stopPullDownRefresh();
    //   }
    // })
  },

  removeItem: function (item) {
    let that = this
    wx.showModal({
      title: '确认',
      content: '您确认要删除此工作室吗？',
      success(res) {
        if (res.confirm) {

          // wx.cloud.callFunction({
          //   name: 'crud',
          //   data: {
          //     op: 'remove',
          //     table: 'studio',
          //     _id: item._id
          //   },
          //   success: res => {
          //     if (res.result.stats.removed == 1) {
          //       that.onLoad()
          //     }
          //   }
          // })
        }
      }
    })
  },
  expireDateChange(e) {
    this.setData({
      'studio.expireDate': e.detail.value
    })
  },
  serviceTypeChange(e) {
    let appid = this.data.serviceTypeList[e.detail.value] == '共享版' ? 'wx53f453674085a5ce' : ''

    this.setData({
      'studio.serviceType': this.data.serviceTypeList[e.detail.value],
      'studio.appid': appid
    })


  },
  apply: function (e) {
    let studio = e.detail.value
    studio._id = this.data.studio._id
    studio.serviceType = this.data.studio.serviceType
    studio.expireDate = this.data.studio.expireDate
    console.log(studio)
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        table: 'studio',
        op: 'update',
        data: studio
      },
      success: res => {
        wx.showToast({
          title: '保存成功',
          icon: 'none'
        })
        this.setData({
          modalname: null
        });
      }
    })
  },
  hideModal() {
    this.setData({
      modalname: null
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.latest > 0) {
      this.data.skip += 20
      this.loadItems({})
    } else {
      wx.showModal({
        content: '没有更多内容了',
        showCancel: false,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})